package com.bbc.wigeth;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import com.bbc.R;

/**
 * Created by Administrator on 2017/9/28.
 */

public class MyRectView extends View {

    private Paint mPaint;
    //初始化宽高
    private int width = 100;
    private int hegth = 100;
    private int x = 100;
    private int y = 100;
    private Context mContext;
    private ImageView mImageView;


    public MyRectView(Context context) {
        super(context, null);
    }

    public MyRectView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        //Paint.ANTI_ALIAS_FLAG 设置抗锯齿
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        //设置抗锯齿
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLUE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.pic);
        canvas.drawBitmap(bitmap, x, y, mPaint);
        mImageView = new ImageView(mContext);
        mImageView.setImageBitmap(bitmap);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                x = (int) event.getX();
                y = (int) event.getY();
                Log.v("cbf", "x==" + x + "y=" + y);
            case MotionEvent.ACTION_UP:
                Animation animation = new AlphaAnimation(0,1);
                animation.setDuration(3000);
                mImageView.startAnimation(animation);
                break;
        }
        WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        int width = windowManager.getDefaultDisplay().getWidth();
        int height = windowManager.getDefaultDisplay().getHeight();
        if (x >= 20 && y >= -20 && x <= width - 20 && y <= height - 90) {
            invalidate();
        }
        return true;
    }
}
